package com.holdtime.jtt809dataservice.client.process;

import com.holdtime.common.utils.general.ApiResponseModel;
import com.holdtime.common.utils.general.BaseResultCode;
import com.holdtime.jtt809dataservice.business.api.ClientLinkService;
import com.holdtime.jtt809dataservice.common.bean.ClientDownDisConnectInFormDTO;
import com.holdtime.jtt809dataservice.common.domain.LinkStatusManager;
import com.holdtime.jtt809dataservice.common.event.ClientMessageReceiveEvent;
import com.holdtime.jtt809dataservice.common.process.ClientProtocolProcessor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import protocol.jtt809.JTT809Packet;
import protocol.jtt809.body.DownDisconnectInform;
import protocol.jtt809.body.RawBody;

/**
 * author: sunwei
 * Date: 2025/3/22 23:34
 * Description:
 */
@Slf4j
@Component
public class ClientDownDisconnectInFormProcessor implements ClientProtocolProcessor {
    @Resource
    private ClientLinkService clientLinkService;

    @Override
    public int getMsgID() {
        return DownDisconnectInform.MSG_ID;
    }

    @Override
    public void process(ClientMessageReceiveEvent event) {
        JTT809Packet<RawBody> source = event.getSource();
        RawBody body = source.getBody();
        DownDisconnectInform downDisconnectInform = new DownDisconnectInform();
        downDisconnectInform.from(body.getRaw());
        ClientDownDisConnectInFormDTO clientDownDisConnectInFormDTO = new ClientDownDisConnectInFormDTO();
        clientDownDisConnectInFormDTO.setErrorCode(downDisconnectInform.getErrorCode());
        clientDownDisConnectInFormDTO.setJym(source.getMsgGnsscenterid());
        ApiResponseModel<?> model = clientLinkService.dealDownDisConnectInForm(clientDownDisConnectInFormDTO);
        if (model.getResultCode() == BaseResultCode.SUCCESS.getCode()) {
            LinkStatusManager.getCurrent().removeMainLinkStatus(source.getMsgGnsscenterid());
        } else {
            log.error("处理上级平台从链路断开通知消息异常,msg{}", model.getMsg());
        }
    }
}
